package com.yunxi.service.dubboService.impl.sys;

import com.github.pagehelper.PageInfo;
import com.yunxi.baseDataToHub.pojo.iam.PmsPostVO;
import com.yunxi.baseDataToHub.service.iam.IamDubboService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.sys.Pos;
import com.yunxi.service.localService.sys.PosService;
import com.yunxi.sys.pojo.PosVO;
import com.yunxi.sys.service.PosDubboService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-20
 */
public class PosDubboServiceImpl implements PosDubboService {
   private static final Logger logger = LoggerFactory.getLogger(PosDubboServiceImpl.class);
   @Autowired
   @Qualifier(value = "posServiceImpl")
   PosService sysPosService;

   @Autowired
   IamDubboService iamDubboServiceImpl;

   @Override
   public PosVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      Pos sysPos = sysPosService.getById(id);
      PosVO PosVO = new PosVO();
      BeanUtils.copyProperties(sysPos, PosVO);
      return PosVO;
   }

   @Override
   @Transactional
   public ApiResult saveOrUpdate(PosVO posVO, LoginInfo loginInfo) throws Exception {
      if (posVO.getType()==null){
         posVO.setType(0);//0店端1厂端
      }
      if (StringUtils.isEmpty(posVO.getPositionId())) {
         posVO.setStatus(1);
      }
      if (StringUtils.isEmpty(posVO.getTenantId())){
         posVO.setTenantId("pms");
      }

      Pos sysPos = new Pos();
      BeanUtils.copyProperties(posVO, sysPos);
      sysPosService.saveOrUpdate(sysPos);

      //店端职位
      if (posVO.getType() == null) return ResponseUtil.fail(-1, "请选择职位类型");
      if (posVO.getType() == 0) {
         List<PmsPostVO> list = new ArrayList<>();
         PmsPostVO pmsPostVO = new PmsPostVO();
         pmsPostVO.setPositionCode(posVO.getPositionCode());
         pmsPostVO.setPositionName(posVO.getPositionName());
         pmsPostVO.setTenantId("");
         pmsPostVO.setStatus(posVO.getStatus());
         pmsPostVO.setOrganizationCode("");
         list.add(pmsPostVO);

         ApiResult apiResult = iamDubboServiceImpl.sendPmsPost(list);
         if (apiResult.getCode().equals(-1)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("  ------- pms2IAM 执行失败 ___渠道职位信息  ---------  ");
            return ResponseUtil.fail(-1, "pms2IAM 执行失败" + apiResult.getMsg());
         }
         return ResponseUtil.ok();
      }
      return ResponseUtil.ok();
   }

   @Override
   public void removeById(String id) {
      sysPosService.removeById(id);
   }

   @Override
   public PageInfo<PosVO> findPageByVO(PosVO PosVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<PosVO> pageInfo = sysPosService.pageByVo(PosVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   public List listByVo(PosVO posVO) {
      return sysPosService.listByVo(posVO);
   }

   @Override
   public void removeByListVO(List<PosVO> orgVoList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
      sysPosService.removeByListVo(orgVoList);
   }
}
